package com.niit.scala

object Base_05 {

  //父类
  class Person(){
    var name:String = _
    var age:Int = _
    def eat() = println("人要吃饭~~~")
  }

  //老师类
  class Teacher extends Person{

    def teach() = println("授课")
  }

  //学生类
  class Student extends Person{

    def study() = println("学习")
  }

  //抽象： abstract class 类名 {}

  //求正方形 和 长方形的面积
  abstract class Shape{
    //抽象方法 ： 只有方法名 和 返回类型
    def area:Double
  }

  //定义一个正方形
  class Square(var edge:Double) extends Shape{
    override def area: Double = edge * edge
}

  //定义一个长方形
  class Rectangle(var length:Double,var width:Double) extends Shape{
    override def area: Double = length * width
  }



  /*
  特质：关键字 trait
  特点：
     特质可以提高代码的复用性
     特质可以提高代码的扩展性和可维护性
     类与特质是继承关系，类与类之间只支持单继承，类与特质之间可以单继承也可以多继承
     特质中可以有普通字段、抽象字段、普通方法、抽象方法
     如果特质中只有抽象内容叫做瘦接口，如果既有抽象又有具体叫做富接口
   */
  //日志接口
  trait Logger{
    def log(msg:String)
  }
  //文件接口
  trait File{
    def write(msg:String,dir:String)
  }
  //类要继承多个特质，特质之间用with关键字
  class ConsoleLogger extends Logger with File {
    override def log(msg: String): Unit = {
      println("当前用户的行为日志："+msg)
    }

    override def write(msg: String, dir: String): Unit = {
      println(s"文件内容：${msg},写入到${dir}")
    }
  }


  /*
   样例类 是一个特殊的类，一般用于保存数据，类似Java中的实体类。
    case class 类名(参数列表)
    变量修饰符默认为val
   */
  case class People(name:String,var age:Int)

  def main(args: Array[String]): Unit = {

    val p = new People("张三",20)

     val teacher = new Teacher
      teacher.name = "小明"
      teacher.age = 32
      teacher.eat()
      teacher.teach()

    val student = new Student
     student.name = "小花"
     student.age = 18
     student.eat()
     student.study()

    val consoleLogger = new ConsoleLogger
    consoleLogger.log("张三登录了澳门博彩网站")
    consoleLogger.write("清明放三天假","/home/niit")

  }

}
